﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Macaw.Wss3.DependencySpiderSolution;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;

namespace Macaw.Wss3.DependencySpiderSolution
{
    public partial class PageDependencies : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        private void ShowDependencies(SPFile file)
        {
            if (file.Exists)
            {
                HtmlGenericControl synonyms = new HtmlGenericControl("UL");
                synonyms.Attributes.Add("class" , "Dependencies");
                MainContent.Controls.Add(synonyms);

                SPSite site = SPContext.Current.Site;

                DependencyMapStorageCollection storagecollection = new DependencyMapStorageCollection(site.WebApplication);

                DependencyMapStorage dependencymapstorage = storagecollection.GetValue<DependencyMapStorage>(site.ID);
                if (dependencymapstorage != null && !string.IsNullOrEmpty(dependencymapstorage.dependencymap))
                {
                    DependencyMap dependencymap = new DependencyMap();
                    StringReader sr = new StringReader(dependencymapstorage.dependencymap);
                    dependencymap.ReadXml(sr);
                    sr.Close();

                    Macaw.Wss3.DependencySpiderSolution.DependencyMap.ResourceRow[] resourcerows = (Macaw.Wss3.DependencySpiderSolution.DependencyMap.ResourceRow[])dependencymap.Resource.Select(string.Format("SPID='{0}'", file.UniqueId));
                    foreach (Macaw.Wss3.DependencySpiderSolution.DependencyMap.ResourceRow resourcerow in resourcerows)
                    {
                        HtmlGenericControl synonym = new HtmlGenericControl("LI");
                        synonym.InnerText = "Resource: " + resourcerow.Url;
                        synonyms.Controls.Add(synonym);

                        HtmlGenericControl dependencies = new HtmlGenericControl("UL");
                        synonym.Controls.Add(dependencies);


                        Macaw.Wss3.DependencySpiderSolution.DependencyMap.DependencyRow[] dependencyrows = resourcerow.GetDependencyRowsByResource_Link_Dependency();
                        foreach (Macaw.Wss3.DependencySpiderSolution.DependencyMap.DependencyRow dependencyrow in dependencyrows)
                        {
                            HtmlGenericControl dependency = new HtmlGenericControl("LI");
                            dependency.InnerText = "Dependency: " ;
                            dependencies.Controls.Add(dependency);

                            HyperLink dependencylink = new HyperLink();
                            dependencylink.NavigateUrl = dependencyrow.ResourceRowByFK_Resource_Source_Dependency.Url;
                            dependencylink.Text = dependencyrow.ResourceRowByFK_Resource_Source_Dependency.Url;
                            dependency.Controls.Add(dependencylink);
                        }

                        //HtmlGenericControl synonymlinks = new HtmlGenericControl("LI");
                        //synonym.Controls.Add(synonymlinks);

                        HtmlGenericControl links = new HtmlGenericControl("UL");
                        synonym.Controls.Add(links);
                        Macaw.Wss3.DependencySpiderSolution.DependencyMap.DependencyRow[] linkrows = resourcerow.GetDependencyRowsByFK_Resource_Source_Dependency(); ;
                        foreach (Macaw.Wss3.DependencySpiderSolution.DependencyMap.DependencyRow linkrow in linkrows)
                        {
                            Macaw.Wss3.DependencySpiderSolution.DependencyMap.ResourceRow linkresourcerow = linkrow.ResourceRowByResource_Link_Dependency;
                            HtmlGenericControl dependency = new HtmlGenericControl("LI");
                            dependency.InnerText = "Link: ";
                            links.Controls.Add(dependency);

                            HyperLink dependencylink = new HyperLink();
                            dependency.Controls.Add(dependencylink);

                            dependencylink.NavigateUrl = linkresourcerow.Url;
                            dependencylink.Text = linkresourcerow.Url;

                            if (!linkrow.IsTypeNull()) dependency.Attributes.Add("class", linkrow.Type);

                            if (linkresourcerow.IsErrorNull())
                            {
                                dependencylink.Attributes.Add("class", "unknown");
                            } else if (linkresourcerow.Error)
                            {
                                dependencylink.Attributes.Add("class", "error");
                                dependencylink.Text += " ("+linkresourcerow.ContentType+")";
                            } 
                        }
                    }


                }
            }                    

        }

        protected override void CreateChildControls()
        {            

            string pageurl = HttpContext.Current.Request.QueryString["pageurl"];
            if (!string.IsNullOrEmpty(pageurl))
            {
                using (SPSite site = new SPSite(pageurl))
                {
                    using (SPWeb web = site.OpenWeb(pageurl, false))
                    {
                        SPFile file = web.GetFile(pageurl);
                        ShowDependencies(file);
                    }
                }
            } else 
            {
                string ItemUrl = HttpContext.Current.Request.QueryString["ItemUrl"];
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
                    {
                        SPFile file = web.GetFile(ItemUrl);
                        ShowDependencies(file);
                    }
                }
            }

            base.CreateChildControls();
        }
    }
}
